import 'package:fate_blind_box/utils/assets.dart';
import 'package:fate_blind_box/utils/loading.dart';
import 'package:fate_blind_box/widget/date_picker/src/date_format.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../../network/Api.dart';
import '../../theme/theme_color.dart';
import '../../utils/provider.dart';

//修改群名
class ChangeGroupName extends StatefulWidget {
  var groupname;

  ChangeGroupName(this.groupname, {Key? key}) : super(key: key);

  @override
  State<ChangeGroupName> createState() => _ChangeGroupNameState();
}

class _ChangeGroupNameState extends State<ChangeGroupName> {
  final TextEditingController _usernameController = TextEditingController();

  String count = "";
  bool canSend = false;
  late String centext;

  // 修改用户信息
  _updataUserinfo(String username) async {
    print("-------------------"+widget.groupname['groupId'].toString());
    if (username.isEmpty || widget.groupname['groupId'].toString().isEmpty) return;
    final res = await Api.getInstance().upDataGroupInfo(widget.groupname['groupId'].toString(),username);

    if (res['code'] == 0) {
      Fluttertoast.showToast(
          msg: "修改成功",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.white,
          textColor: Colors.black,
          fontSize: 14.0);
      Navigator.of(context).pop('1');

    } else {
      if (kDebugMode) {
        toast(res['msg']);
        print('------获取用户异常---------------' + res['msg']);
      }
    }
  }

  void validateInput(String test) {
    setState(() {
      canSend = test.isNotEmpty;
      centext = test;
    });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getCommonAppBar("", color: Colors.black),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Flexible(
              child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                margin: const EdgeInsets.only(top: 42),
                child: const Text(
                  '修改群聊名称',
                  style: TextStyle(
                      color: Colors.black,
                      fontSize: 22,
                      fontWeight: FontWeight.bold),
                ),
              ),
            ],
          )),
          Container(
            margin: const EdgeInsets.only(top: 20),
            child: const Text(
              '修改群聊名称后，将在群内通知其他成员。',
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 16,
                  fontWeight: FontWeight.normal),
            ),
          ),
          Container(
              margin: const EdgeInsets.only(left: 30, right: 30,top: 30),
              child: Row(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(3),
                    child: Image.network(
                      Provider.fixAvatar(widget.groupname['groupUrl'] ?? ""),
                      width: 38,
                      height: 38,
                      fit: BoxFit.fill,
                    ),
                  ),
                  Container(
                    width: 200,
                    margin: const EdgeInsets.only(top: 10,left: 20),
                    child: TextField(
                      controller: _usernameController,
                      inputFormatters: [
                        FilteringTextInputFormatter.allow(
                            RegExp("[\u4e00-\u9fa5]")),
                      ],
                      decoration: const InputDecoration(
                        border: UnderlineInputBorder(
                          borderRadius: BorderRadius.all(
                            Radius.circular(1), //边角为30
                          ),
                          borderSide: BorderSide(
                            color: Colors.grey, //边线颜色为黄色
                            width: 2, //边线宽度为2
                          ),
                        ),
                        hintText: "请输入新的群名称",
                      ),
                      onChanged: validateInput,
                    ),
                  ),
                ],
              )),
          InkWell(
            onTap: (){
              if(centext.isNotEmpty){
                _updataUserinfo(centext);
              }
            },
            child: Container(
              margin: const EdgeInsets.only(left: 7,top: 50),
              padding:
              const EdgeInsets.only(left: 50, right:50, top: 10, bottom: 10),
              decoration: BoxDecoration(
                color: canSend?ThemeColor.sendimTextColor:ThemeColor.checkNormalColor,
                borderRadius: const BorderRadius.all(Radius.circular(3)),
              ),
              child: Text(
                "确定",
                style: TextStyle(
                    color: canSend?Colors.white:ThemeColor.textColorSecond,
                    fontSize: 18,
                    fontWeight: FontWeight.normal),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
